home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9203.ZIP / OOPASM.ZIP / OBJECTS.ASM < prev    next >
Assembly Source File  |  1990-12-14  |  8KB  |  294 lines

  1.     .MODEL    SMALL
  2.  
  3.     INCLUDE    equates.inc
  4.  
  5.     CombinedMthSpace    EQU    400h    ;Amount of space to allocate
  6.                         ;  for combined methods.
  7.                         ;  Depends on number of
  8.                                                 ;  messages.
  9.  
  10. IF1
  11.     INCLUDE    macros.mac
  12.     INCLUDE    objects.mac
  13. ENDIF
  14.  
  15.     .CODE
  16.  
  17.     PUBLIC    initObject
  18. COMMENT %
  19. =============================================================================
  20. Initializes an object by flattening its inheritance lattice to create 
  21. combined methods for its messages.
  22.  
  23. Passed:    si - Addr ptr to object structure
  24.  
  25. =============================================================================%
  26. initObject    PROC    NEAR
  27.     lea        di,Buffer        ;Get buffer addr
  28.     call        findAncestors        ;Find/Save all ancestors
  29.     call        evalMsgs        ;Evaluate messages
  30.     ret
  31. initObject    ENDP
  32.  
  33.  
  34.  
  35. COMMENT %
  36. =============================================================================
  37. Finds all of an object's ancestors and saves them for use by the message 
  38. evaluator.
  39.  
  40. Passed:    bx - Addr ptr to message table (end of object table)
  41.     di - Addr ptr to temporary object table
  42.     si - Addr ptr to object structure
  43.  
  44. =============================================================================%
  45. findAncestors    PROC    NEAR
  46.     pushData    <bx,si>            ;Save obj ptr
  47.     mov        bx,Wptr[si].Messages    ;Get addr ptr to msg tbl
  48.     mov        si,Wptr[si].Objects    ;Get addr of object tbl
  49.     movsw                    ;Move obj ptr
  50. fas1:    eq        bx,si,fas2        ;Exit if end of tbl
  51.     push        si
  52.     mov        si,Wptr[si]        ;Get next object
  53.     call        findAncestors        ;Find others
  54.     pop        si
  55.     add        si,2
  56.     jmp        fas1            ;More in tbl - Loop
  57. fas2:    mov        Wptr[di],Nil        ;Mark end of list
  58.     popData        <si,bx>            ;Restore obj ptr
  59.     ret
  60. findAncestors    ENDP
  61.  
  62.  
  63.  
  64. COMMENT %
  65. =============================================================================
  66. Creates combined methods for all of an object's messages.
  67.  
  68. Passed:    si - Addr ptr to object structure
  69.  
  70. =============================================================================%
  71. evalMsgs    PROC    NEAR
  72.     mov        bx,Wptr[si].Messages    ;Get addr of message tbl
  73.     mov        cx,Wptr[si].Instances    ;Get addr of instance tbl
  74. ems1:    mov        dl,Bptr[bx]        ;Get msg number
  75.     xor        dh,dh
  76.     call        combineMethods        ;Combine methods
  77.     add        bx,3            ;Point to next tbl entry
  78.     neq        bx,cx,ems1        ;More in tbl? - loop
  79.     ret
  80. evalMsgs    ENDP
  81.  
  82.  
  83.  
  84. COMMENT %
  85. =============================================================================
  86. Combines methods for all included objects.
  87.  
  88. Passed:    dx - Message number
  89.     si - Addr ptr to object structure
  90.  
  91. =============================================================================%
  92. combineMethods    PROC    NEAR
  93.     push        bx
  94.     mov        ?Compiled,Nil        ;Clear compiled flag
  95.     mov        bx,Wptr[CompileStart]    ;Get start of combined mthd
  96.     mov        Wptr[CompilePtr],bx    ;Init location ptr
  97.  
  98.     mov        di,Nil            ;Zero count word
  99.     call        saveMethodAddr        ;Save value
  100.  
  101.     call        saveBefores        ;Save Before methods
  102.  
  103.     mov        bx,Primary        ;Select Primary method type
  104.     lea        di,Buffer        ;Get addr of tmp object tbl
  105.     mov        di,Wptr[di]        ;Get tbl entry
  106.     call        saveMethod        ;Save method
  107.  
  108.     call        saveAfters        ;Save After methods
  109.  
  110.     null        ?Compiled,cms1        ;Nothing compiled? - Exit
  111.     call        updatePtrs        ;Update message, location ptrs
  112. cms1:    pop        bx
  113.     ret
  114. combineMethods    ENDP
  115.  
  116.  
  117.  
  118. COMMENT %
  119. =============================================================================
  120. Updates the message and location pointers.
  121.  
  122. Passed:    dx - Message number
  123.     si - Addr ptr to object structure
  124.  
  125. =============================================================================%
  126. updatePtrs    PROC    NEAR
  127.     push        si
  128.     findMsg        si,dl            ;Find message
  129.     mov        di,Wptr[CompileStart]    ;Get ptr to combined method
  130.     mov        Wptr[si],di        ;Change message ptr
  131.  
  132.     mov        bx,Wptr[CompilePtr]    ;Get current compile location
  133.     mov        Wptr[CompileStart],bx    ;Reset start of combined mthd 
  134.     pop        si
  135.     ret
  136. updatePtrs    ENDP
  137.  
  138.  
  139.  
  140. COMMENT %
  141. =============================================================================
  142. Save the Before method type for the specified object.
  143.  
  144. Passed:    dx - Message number
  145.  
  146. =============================================================================%
  147. saveBefores    PROC    NEAR
  148.     push        si
  149.     mov        bx,Before        ;Select Before method type
  150.     lea        si,Buffer        ;Get addr of tmp object tbl
  151.     mov        di,Wptr[si]        ;Get tbl entry
  152. sbs1:    call        saveMethod        ;Save method
  153.     add        si,2            ;Point to next tbl entry
  154.     mov        di,Wptr[si]        ;Get next tbl entry
  155.     identity    di,sbs1            ;More in table? - loop
  156.     pop        si
  157.     ret
  158. saveBefores    ENDP
  159.  
  160.  
  161.  
  162. COMMENT %
  163. =============================================================================
  164. Save the After method type for the specified object.
  165.  
  166. Passed:    dx - Message number
  167.  
  168. =============================================================================%
  169. saveAfters    PROC    NEAR
  170.     pushData    <cx,si>
  171.     mov        bx,After        ;Select After method type
  172.     lea        si,Buffer        ;Get addr of tmp object tbl
  173.     mov        cx,si            ;Save addr of object tbl
  174. sas1:    mov        ax,Wptr[si]        ;Get tbl entry
  175.     null        ax,sas2            ;Null? - End of tbl, exit
  176.     add        si,2            ;Point to next tbl entry
  177.     jmp        sas1            ;Loop
  178.  
  179. sas2:    sub        si,2            ;Point to previous tbl entry
  180.     mov        di,Wptr[si]        ;Get next tbl entry
  181.     call        saveMethod        ;Save method
  182.     neq        si,cx,sas2
  183.     popData        <si,cx>
  184.     ret
  185. saveAfters    ENDP
  186.  
  187.  
  188.  
  189. COMMENT %
  190. =============================================================================
  191. Save the specified method for specified object.
  192.  
  193. Passed:    bx - Method type
  194.     di - Addr ptr to object structure
  195.     dx - Message number
  196.  
  197. =============================================================================%
  198. saveMethod    PROC    NEAR
  199.     pushData    <bx,di,si>
  200.     findMsg        di,dl,svm3        ;Find message
  201.     mov        di,Wptr[si]        ;Get method tbl addr ptr
  202.     null        di,svm3            ;Exit if no local methods
  203.     mov        di,Wptr[di+bx]        ;Get method addr ptr
  204.     null        di,svm3            ;Exit if no message
  205.  
  206.     mov        bx,Wptr[CompileStart]    ;Get start of combined mthd
  207. svm1:    eq        bx,Wptr[CompilePtr],svm2
  208.     eq        di,Wptr[bx],svm3    ;Exit if duplicate method
  209.     add        bx,2            ;Point to next addr
  210.     jmp        svm1            ;Check next addr
  211.  
  212. svm2:    call        saveMethodAddr        ;Save method addr
  213. svm3:    popData        <si,di,bx>
  214.     ret
  215. saveMethod    ENDP
  216.  
  217.  
  218.  
  219. COMMENT %
  220. =============================================================================
  221. Save value at current compile location, and increments location pointer.
  222.  
  223. Passed:    di - Value to store
  224.  
  225. =============================================================================%
  226. saveMethodAddr    PROC    NEAR
  227.     mov        ?Compiled,1        ;Set compiled flag
  228.     mov        bx,Wptr[CompilePtr]    ;Get ptr to combined mthd end
  229.     mov        Wptr[bx],di        ;Save value
  230.     add        bx,2            ;Point to next location
  231.     mov        Wptr[CompilePtr],bx    ;Reset location ptr 
  232.  
  233.     mov        bx,Wptr[CompileStart]    ;Get ptr mthd count
  234.     mov        di,Wptr[bx]        ;Get mthd count
  235.     inc        di            ;Increments mthd count
  236.     mov        Wptr[bx],di        ;Save value
  237.     ret
  238. saveMethodAddr    ENDP
  239.  
  240.  
  241.  
  242.     PUBLIC    sendMsg
  243. COMMENT %
  244. =============================================================================
  245. Sends the specified object the given message. This causes the execution of 
  246. the combined message for the object.
  247.  
  248. Passed:    dx - Message number
  249.     si - Combined method ptr
  250.  
  251. =============================================================================%
  252. sendMsg    PROC    NEAR
  253.     mov        Wptr[Self],si        ;Set Self variable
  254.     findMsg        si,dl,smg2        ;Search for message
  255.     mov        si,Wptr[si]        ;Get method addr
  256.     mov        cx,Wptr[si]        ;Get method count
  257. smg1:    add        si,2            ;Point to method
  258.     pushData    <cx,si>            ;Save loop cnt, addr ptr
  259.     call        Wptr[si]        ;Execute method
  260.     popData        <si,cx>            ;Restore addr ptr, loop cnt
  261.     loop        smg1            ;Loop
  262. smg2:    ret
  263. sendMsg    ENDP
  264.  
  265.  
  266.  
  267.     .DATA
  268.  
  269. INCLUDE    objects.inc
  270.  
  271.     PUBLIC    Buffer
  272.     PUBLIC    Self
  273. IF Dbug
  274.     PUBLIC    ?Compiled
  275.     PUBLIC    CompilePtr
  276.     PUBLIC    CompileStart
  277. ENDIF
  278. Self        DW    Nil            ;Object variable
  279. ?Compiled    DW    Nil            ;Flag indicating wether
  280.                         ;  method addresses were
  281.                         ;  compiled for message.
  282. CompilePtr    DW    Nil            ;Points to next location to
  283.                         ;  compile method address.
  284. CompileStart    DW    $+2            ;Points to beginning of
  285.                         ;  combined method.
  286.         DB    CombinedMthSpace DUP (Nil)
  287.                         ;Allocate space for combined
  288.                                                 ;  method tables.
  289. Buffer        DB    BufSize DUP (Nil)    ;Buffer for text input and
  290.                         ;  temporay ancestor tables
  291.                                                 ;  during initialization.
  292.  
  293.     END
  294.